//
//  WZSettingCell.m
//  ETong
//
//  Created by wanzhao on 15/11/2.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "WZSettingCell.h"

@implementation WZSettingCell

+ (instancetype)settingCellWithTableView:(UITableView *)tableView
{
    static NSString *setId = @"settingCell";
    WZSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:setId];
    if (cell == nil) {
        cell = [[WZSettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:setId];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UISwitch *switchBtn = [[UISwitch alloc] init];
        [switchBtn addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
        [self.contentView addSubview:switchBtn];
        self.switchBtn = switchBtn;
        
        UILabel *titLabel = [[UILabel alloc] init];
        [self.contentView addSubview:titLabel];
        self.titLabel = titLabel;
    }
    
    return self;
}


- (void)setIsOn:(BOOL)isOn
{
    _isOn = isOn;
    self.switchBtn.on = isOn;
}
- (void)valueChange:(UISwitch *)switchBtn
{
    if ([self.settingDelegate respondsToSelector:@selector(settingCellDidChangeValue:withIndexPath:)]) {
        [self.settingDelegate settingCellDidChangeValue:switchBtn.isOn withIndexPath:self.cellIndexPath];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.switchBtn.x = self.width - OppositeWidthRate(10) - self.switchBtn.width;
    self.switchBtn.y = (self.height - self.switchBtn.height) / 2.0f;
    self.titLabel.frame = CGRectMake(OppositeWidthRate(20), 0, self.switchBtn.x - OppositeWidthRate(10) - OppositeWidthRate(20), self.height);
}
@end
